package com.ada.base.security.controller;

import com.ada.base.common.ApiResponse;
import com.ada.base.common.ControllerResponse;
import com.ada.base.security.entity.Role;
import com.ada.base.security.entity.RoleAuthority;
import com.ada.base.security.entity.res.RoleRes;
import com.ada.base.security.service.RoleService;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "角色")
@RequestMapping("base/role")
@RestController
public class BaseRoleController extends ControllerResponse {
    private RoleService roleService;

    public BaseRoleController(RoleService roleService) {
        this.roleService = roleService;
    }

//    @GetMapping("/{userId}")
    public ApiResponse<List<RoleRes>> roleList(@PathVariable("userId") long userId) {
        return ok(roleService.roleList(userId));
    }

    @GetMapping("/{userId}/{type}")
    public ApiResponse<List<RoleRes>> roleList(@PathVariable("userId") long userId, @PathVariable("type") int type) {
        return ok(roleService.roleList(userId, type));
    }

    @PostMapping
    public ApiResponse<Integer> add(@RequestBody Role role) {
        return ok(roleService.add(role));
    }

    @DeleteMapping("/{id}")
    public ApiResponse<Object> delete(@PathVariable("id") long id) {
        roleService.delete(id);
        return ok(null);
    }

    @PostMapping("alloc")
    public ApiResponse<Object> allocRole(@RequestBody List<RoleAuthority> roleAuthorities) {
        roleService.allocAuthority(roleAuthorities);
        return ok();
    }
}
